package com.assessment.servermanager.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.assessment.servermanager.commen.constant.Constants;
import com.assessment.servermanager.commen.entity.AjaxResult;
import com.assessment.servermanager.commen.entity.model.LoginBody;
import com.assessment.servermanager.service.ILoginService;
import com.assessment.servermanager.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private ITbUserService userService;

    @PostMapping("/login")
    @SaIgnore
    public AjaxResult login(@RequestBody LoginBody loginBody){
        Map<String, Object> ajax = new HashMap<>();
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode());
        ajax.put(Constants.TOKEN, token);
        return AjaxResult.success(ajax);
    }

    /**
     * 退出登录
     */
    @SaIgnore
    @PostMapping("/logout")
    public AjaxResult logout() {
        loginService.logout();
        return AjaxResult.success("退出成功");
    }

}
